<?php

/**
 * This is the model class for table "tbl_user".
 *
 * The followings are the available columns in table 'tbl_user':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property integer $role_id
 * @property string $email
 * @property string $profile
 * @property string $create_date
 * @property string $update_date
 */
class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules(){
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username', 'required','message'=>'登录名不能为空'),
            array('username', 'unique','message'=>'登录名已经存在'),
            array('sex', 'required','message'=>'性别不能为空'),
            array('password', 'required','message'=>'登录密码不能为空'),
            array('email', 'required','message'=>'Email地址不能为空'),
            array('email', 'unique','message'=>'Email地址已经注册'),
            array('email', 'email','message'=>'Email地址格式错误'),
            array('role_id', 'required', 'message'=>'角色类型不能为空'),
			array('role_id', 'numerical', 'integerOnly'=>true),
			array('username, password, email', 'length', 'max'=>128),
			array('profile, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, role_id, qq, weibo, phone, email, sex, profile, create_date, update_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'role'=>array(self::BELONGS_TO, 'Type', 'role_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => '登录名',
			'password' => '登录密码',
			'role_id' => '角色类型',
			'email' => 'Email地址',
            'qq' => 'QQ帐号',
            'weibo' => '微博帐号',
            'sex' => '选择性别',
            'phone' => '手机号',
			'profile' => '简介',
			'create_date' => '创建日期',
			'update_date' => '更新日期',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('role_id',$this->role_id);
		$criteria->compare('email',$this->email,true);
        $criteria->compare('qq',$this->qq,true);
        $criteria->compare('weibo',$this->weibo,true);
        $criteria->compare('sex',$this->sex,true);
        $criteria->compare('phone',$this->phone,true);
		$criteria->compare('profile',$this->profile,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('update_date',$this->update_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    /**
	 * 行为操作
	 */
	public function behaviors(){
		return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_date',
                'updateAttribute' => 'update_date',
                'setUpdateOnCreate' => 'true',
            )
		);
	}
    
    public function beforeSave(){
        if(parent::beforeSave()){
            $this->setAttribute('password',  AdminIdentity::md5Hash($this->password));
            $this->setAttribute('qq',  empty($this->qq) ? '暂无' : $this->qq);
            $this->setAttribute('weibo',  empty($this->weibo) ? '暂无' : $this->weibo);
            $this->setAttribute('phone',  empty($this->phone) ? '暂无' : $this->phone);
            
        }
        return true;
    }
    
    /**
     * 验证用户名是否存在
     */
}